package likou;

/**
 * @Info:
 * @Author: 唐小尊
 * @Date: 2020/7/7  19:35
 */
public class _5 {
    public static void main(String[] args) {
        String s = "babad";
        System.out.println(longestPalindrome(s));
    }

    public static String longestPalindrome(String s) {
        String max = "";
        for (int i = 0; i < s.length(); i++) {
            String nowStr = getMax(s, i);
            if (nowStr.length() > max.length()) {
                max = nowStr;
            }
        }
        return max;
    }

    public static String getMax(String s, int now) {
        byte[] bytes = s.getBytes();
        int left = now - 1;
        int right = now + 1;
        while (left != -1 && bytes[left] == bytes[now]) {
            left--;
        }
        while (right != s.length() && bytes[right] == bytes[now]) {
            right++;
        }
        while (true) {
            if (left != -1 && right != s.length()) {
                if (bytes[left] == bytes[right]) {
                    right++;
                    left--;
                } else {
                    right++;
                    return s.substring(left + 1, right-1);
                }
            } else {
                return s.substring(left + 1, right);
            }
        }

    }
}
